package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 获取当前登录用户基本信息
     * TODO 只有接口中需要获取当前登录用户ID，使用@GuiGuLogin注解
     *
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "获取当前登录用户基本信息")
    @GetMapping("/wxLogin/getUserInfo")
    public Result<UserInfoVo> getUserInfo() {
        //1.获取当前登录用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务逻辑获取用户信息
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }


    /**
     * 更新当前用户基本信息
     *
     * @param userInfoVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "更新当前用户基本信息")
    @PostMapping("/wxLogin/updateUser")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo) {
        //1.获取当前登录用户ID
        //Long userId = AuthContextHolder.getUserId();
        //2.调用业务逻辑获取用户信息
        userInfoService.updateUser(userInfoVo);
        return Result.ok();
    }


    /**
     * 根据指定用户ID查询用户基本信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据指定用户ID查询用户基本信息")
    @GetMapping("/userInfo/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }


    /**
     * 提供给专辑服务动态判断声音付费标识业务接口，其中请求体参数声音ID集合，每一页中声音ID都需要提交来进行验证购买情况
     *
     * @param userId               用户ID
     * @param albumId              专辑ID
     * @param needCheckTrackIdList 待检查购买情况声音ID集合
     * @return {专辑ID:购买结果}   1：已购   0：未购买
     */
    @Operation(summary = "根据用户ID+专辑ID+声音ID列表动态判断声音购买情况")
    @PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(
            @PathVariable Long userId,
            @PathVariable Long albumId,
            @RequestBody List<Long> needCheckTrackIdList
    ) {
        Map<Long, Integer> map = userInfoService.userIsPaidTrack(userId, albumId, needCheckTrackIdList);
        return Result.ok(map);
    }


    /**
     * 验证当前用户是否购买过指定专辑
     *
     * @param albumId
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "验证当前用户是否购买过指定专辑")
    @GetMapping("/userInfo/isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
        Boolean isPaid = userInfoService.isPaidAlbum(albumId);
        return Result.ok(isPaid);
    }


    /**
     * 查询当前用户某个专辑中已购声音id集合
     *
     * @param albumId
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "查询当前用户某个专辑中已购声音id集合")
    @GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
    public Result<List<Long>> getUserPaidTrackIdList(@PathVariable Long albumId) {
        List<Long> list = userInfoService.getUserPaidTrackIdList(albumId);
        return Result.ok(list);
    }


    /**
     * 提交订单支付（余额，支付宝）成功-虚拟物品发货
     *
     * @param userPaidRecordVo
     * @return
     */
    @Operation(summary = "提交订单支付（余额，支付宝）成功-虚拟物品发货")
    @PostMapping("/userInfo/savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }



    /**
     * 更新VIP状态：处理过期会员
     * @return
     */
    @Operation(summary = "更新VIP状态：处理过期会员")
    @GetMapping("/updateVipExpireStatus")
    public Result updateVipExpireStatus(){
        userInfoService.updateVipExpireStatus(new Date());
        return Result.ok();
    }
}

